package com.example.demo.test;


import com.example.demo.domain.User;
import com.example.demo.utils.FileUtils;
import org.apache.pdfbox.multipdf.Splitter;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.junit.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

@SpringBootTest
public class ZipFiles

{
    private static final Pattern PATTERN = Pattern.compile("\\d+");
    @Test
    public void fileToZip(){
        File file = new File("F:/adi/b.pdf");

        File zipFile = new File("F:/adi/20210809.zip");

        try {
            //文件转为输入流
            FileInputStream fileInputStream = new FileInputStream(file);
            //压缩包作为输出流
            ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile));

            zipOutputStream.putNextEntry(new ZipEntry(file.getName()));

            int temp = 0;

            while((temp = fileInputStream.read()) != -1){
                //把输入流读到的内容写道输出流
                zipOutputStream.write(temp);
            }
            //关闭流
            fileInputStream.close();

            zipOutputStream.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Test
    public void filesToZip(){
        File file = new File("F:/adi/a.pdf");
        File file1 = new File("F:/adi/b.pdf");

        HashSet<File> fileSet = new HashSet<>();
        fileSet.add(file);
        fileSet.add(file1);

         File zipFile = new File("F:/adi/20210814.zip");

        try {

            //压缩包作为输出流
            ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile));

            for(File f : fileSet){
                //文件转为输入流
                FileInputStream fileInputStream = new FileInputStream(f);

                zipOutputStream.putNextEntry(new ZipEntry(f.getName()));

                int temp = 0;

                while((temp = fileInputStream.read()) != -1){
                    //把输入流读到的内容写道输出流
                    zipOutputStream.write(temp);
                }
                //关闭流
                fileInputStream.close();
            }
            zipOutputStream.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 压缩文件夹
     */
    @Test
    public void catalogueToZip(){
        File file = new File("F:/adi/zip");

        File zipFile = new File("F:/adi/20210801.zip");

        try {
            //压缩包作为输出流
            ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile));

            for (File listFile : file.listFiles()) {
                //文件转为输入流
                FileInputStream fileInputStream = new FileInputStream(listFile);

                zipOutputStream.putNextEntry(new ZipEntry(listFile.getName()));

                int temp = 0;

                while((temp = fileInputStream.read()) != -1){
                    //把输入流读到的内容写道输出流
                    zipOutputStream.write(temp);
                }
                //关闭流
                fileInputStream.close();
            }
            zipOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void resoveString(){
        System.out.println(System.currentTimeMillis()/1000);
        System.out.println(Calendar.getInstance().getTimeInMillis());

        Long timestamp = System.currentTimeMillis()/1000;
        System.out.println(timestamp);
    }

    @Test
    public void newPdf(){
        File file = new File("C:/Users/msn/Desktop/0122609GDM 外箱标签 一式两份 一箱贴两张.pdf");
        PDDocument document = null;
        try {
            document = PDDocument.load(file);

            // 按页切割文档
            Splitter splitter = new Splitter();

            List<PDDocument> documentList = splitter.split(document);
            for (PDDocument pdDocument : documentList) {
                PDPage myPage = pdDocument.getPage(0);
                document.addPage(myPage);
            }

            document.save("D:/34m.pdf");


        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    @Test
    public void testFileUtils(){
        File derectory = new File("F:\\adi\\zip");
        File zipFile = new File("F:\\adi\\adi.rar");
        List<File> fileList = new ArrayList<>();
        if(derectory.isDirectory()){
            for (File file : derectory.listFiles()) {
                fileList.add(file);
            }

        }

        File file = FileUtils.zipFiles(fileList, zipFile);
        System.out.println(file.getAbsolutePath());

    }

    @Test
    public void testCopyFile(){
        File file = new File("F:\\adi\\a.txt");
        String src = "E:\\b.txt";

        File targetFile = FileUtils.copyFile(file,src,8L);
        System.out.println(targetFile.length());

    }

    @Test
    public void testMutipleZero(){

        HashMap<String, ArrayList<Integer>> m = new HashMap<>();
        ArrayList<Integer> arrayList = new ArrayList<>();
        arrayList.add(1);
        m.put("a",arrayList);
        ArrayList<Integer> list = m.get("a");
        System.out.println(m);
        list.add(2);
        System.out.println(m);


        Integer a = 0;
        boolean b = a != null;
//        boolean c = a != "";
    }

    @Test
    public void testContinue(){
        String[] strings = new String[]{"a","b","c"};
        for (String s : strings) {
            int a = 5;
            System.out.println(a);
            if ("a".equals(s)) {
                if (true) {
                    continue;
                }
                System.out.println("if");
            }
            System.out.println(a+10);
        }
    }

    @Test
    public void testListToArray(){
        ArrayList<String> list = new ArrayList<>();

        String[] strings = new String[list.size()];
        strings = list.toArray(strings);
    }
    @Test
    public void testList(){
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
//        Iterator<String> iterator = list.iterator();
//        while (iterator.hasNext()) {
//            if("1".equals(iterator.next())){
//                iterator.remove();
//            }
//        }
//        for (String item : list) {
//            if ("2".equals(item)) {
//                list.remove(item);
//            }
//        }
//        System.out.println(list);

    }

    @Test
    public void testSet(){
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("2");
        // 1
        Set<String> stringSet = new HashSet<>(list);
        List<String> strings = new ArrayList<>(stringSet);
        // 2
        List<String> collect = list.stream().distinct().collect(Collectors.toList());
    }

    @Test
    public void testBlank() {
        String s = " as dad";
        String b = "　ada JJJ";
        System.out.println(s.trim());
        System.out.println(b.trim());
        System.out.println(b.length());
        while (b.startsWith("　")){
            b = b.substring(1).trim();
            while (b.endsWith("　")){
                b = b.substring(0,b.length()-1).trim();
            }
        }
        System.out.println(b);

    }

    @Test
    public void testCollections() {
        List<String> list = null;
        if(CollectionUtils.isEmpty(list)){
            System.out.println("你大爷");
        }
//        if(list.isEmpty()){
//            System.out.println("isEmpty 方法会有空指针隐患");
//        }
        Map<String, String> map = new HashMap<>(16);
        System.out.println(map instanceof  HashMap);
        System.out.println(map instanceof  Map);
        System.out.println(map instanceof AbstractMap);
        System.out.println(map instanceof TreeMap);

    }


    @Test
    public void testxx(){
        int x = 125;

        String str = String.valueOf(x);
        List<Integer> list = new ArrayList<>();
        // 3
        for(int i= 0; i<str.length();i++){
            Integer item = Integer.valueOf(str.substring(i,i+1));
            list.add(item);
        }
        System.out.println(list);
        User target = new User();

        target.setId(1);
        target.setName("张三");
        target.setScore(89);

        User source = new User();
        source.setName("李四");

        BeanUtils.copyProperties(source,target);
        System.out.println(source);
        System.out.println(target);
//        Array a = Array.newInstance(,5);
int[] b = new int[]{1, 2, 3};

int[] c = new int[5];


    }


}
